package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.*;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.ruoyi.common.bean.chart.LineChartBean;
import com.ruoyi.common.bean.chart.LineChartDataBean;
import com.ruoyi.common.bean.log.SysLoginInfoVo;
import com.ruoyi.common.enums.chart.ChartTypeEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.convert.sys.SysLoginInfoConvert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.SysLoginInfo;
import com.ruoyi.system.mapper.SysLogininforMapper;
import com.ruoyi.system.service.ISysLogininforService;

/**
 * 系统访问日志情况信息 服务层处理
 * 
 * @author ruoyi
 */
@Slf4j
@Service
public class SysLogininforServiceImpl implements ISysLogininforService
{

    @Autowired
    private SysLogininforMapper logininforMapper;

    /**
     * 新增系统登录日志
     * 
     * @param logininfor 访问日志对象
     */
    @Override
    public void insertLogininfor(SysLoginInfoVo logininfor)
    {
        SysLoginInfo loginInfo = SysLoginInfoConvert.INSTANCE.convert(logininfor);
        logininforMapper.insertLogininfor(loginInfo);
    }

    /**
     * 查询系统登录日志集合
     * 
     * @param logininfor 访问日志对象
     * @return 登录记录集合
     */
    @Override
    public List<SysLoginInfo> selectLogininforList(SysLoginInfo logininfor)
    {
        return logininforMapper.selectLogininforList(logininfor);
    }

    /**
     * 批量删除系统登录日志
     * 
     * @param infoIds 需要删除的登录日志ID
     * @return 结果
     */
    @Override
    public int deleteLogininforByIds(Long[] infoIds)
    {
        return logininforMapper.deleteLogininforByIds(infoIds);
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public void cleanLogininfor()
    {
        logininforMapper.cleanLogininfor();
    }

    @Override
    public LineChartBean getLoginLineChart() {

        SysLoginInfo logininfor = new SysLoginInfo();
        Map<String, Object> params = new HashMap<>();
        params.put("beginTime", DateUtil.format(DateUtils.getDataLastN(7),DateUtils.YYYY_MM_DD_HH_MM_SS));
        logininfor.setParams(params);

        List<SysLoginInfo> logins = logininforMapper.selectLogininforList(logininfor);
        List<String> xaxisData = DateUtils.getLat7();
        Map<String, Integer> xAxisDataMap = new HashMap<>();

        logins.forEach(item -> {
            String date = DateUtils.dateTime(item.getLoginTime());
            xAxisDataMap.merge(date, 1, Integer::sum);
        });


        List<BigDecimal> data = new ArrayList<>();
        xaxisData.forEach(item -> {
            data.add(NumberUtil.toBigDecimal(xAxisDataMap.get(item)));
        });

        LineChartDataBean lineChartDataBean = new LineChartDataBean();
        lineChartDataBean.setName(ChartTypeEnum.CHART_LOGIN_STATISTICS.getValue());
        lineChartDataBean.setData(data);

        List<LineChartDataBean> yaxisData = new ArrayList<LineChartDataBean>();
        yaxisData.add(lineChartDataBean);

        LineChartBean lineChartBean = new LineChartBean();
        lineChartBean.setXaxisData(xaxisData);
        lineChartBean.setYaxisData(yaxisData);

        return lineChartBean;
    }
}
